package cn.edu.hznu;

import cn.edu.hznu.dao.IUserDao;
import cn.edu.hznu.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;



import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wjj on 2020/3/30
 */
public class CRUDtest {
    private  InputStream in=null;
    private  SqlSessionFactory sqlSessionFactory=null;
    SqlSession session=null;
    IUserDao userDao=null;
    @Before
    public void init()throws  Exception{
      in= Resources.getResourceAsStream("SqlMapConfig.xml");
      sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
      session=sqlSessionFactory.openSession();
      userDao=session.getMapper(IUserDao.class);
    }
    @After
    public  void destroy()throws  Exception{
        session.commit();
        session.close();
        in.close();
    }

    /*
    * 测试查询所有用户并显示所有用户所拥有的账号信息
    * */
    @Test
    public void  TestfindAll(){

        List<User>users=userDao.findAll();
        for(User user:users){
            System.out.println(user);
        }

    }

    /*
     * 查询单个用户根据id,并显示该用户所属的账号属性
     * */
    @Test
    public void  TestfindUserById(){

        User user=userDao.findUserById(46);
        System.out.println(user);
    }


}
